package com.jt.tenant.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jt.tenant.R;
//import com.jt.tenant.activity.NewVCISelectActivity;
import com.jt.tenant.activity.InsSelectActivity;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetInsList;
import com.jt.tenant.widget.CustomSpinner;
import com.jt.tenant.widget.InsSelectSpinner;
//import com.jt.tenant.dialog.VCINumDialog;
//import com.jt.tenant.widget.CustomSpinner;
//import com.jt.tenant.widget.VCISpinner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/7.
 */
public class InsSelectAdapter extends BaseAdapter {

    private Context context;

    private ViewHolder viewHolder = null;

    private List<GetInsList.INS_ITEM> list = new ArrayList<>();

    public static RefreshLinster refreshLinster;

    public InsSelectAdapter(Context context, final List<GetInsList.INS_ITEM> list) {

        this.context = context;

        this.list.clear();

        this.list = list;

    }

    @Override
    public int getCount() {

        return list.size();

    }

    @Override
    public Object getItem(int position) {

        return list.get(position);

    }

    @Override
    public long getItemId(int position) {

        return position;

    }

    @Override
    public int getItemViewType(int position) {

        switch (list.get(position).TYPE) {

//            0:下拉选项，1：填写数量，2：直接购买
            case "1":

                return 2;

            case "2":

                return 0;

            case "3":

                return 1;

            default:

                return 3;

        }

    }

    @Override
    public int getViewTypeCount() {

        return 3;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        int type = getItemViewType(position);

        if (convertView == null) {

            convertView = View.inflate(context, R.layout.item_ins_select, null);

            JTApplication.ScaleScreenHelper.loadView((ViewGroup) convertView);

            viewHolder = new ViewHolder();

            viewHolder.name = (TextView) convertView.findViewById(R.id.tv_ins_select_name);

            viewHolder.buy = (ImageView) convertView.findViewById(R.id.iv_ins_select_buy);

            viewHolder.instruction = (TextView) convertView.findViewById(R.id.tv_ins_select_instruction);

            viewHolder.suggestion = (TextView) convertView.findViewById(R.id.tv_ins_select_suggesttion);

            viewHolder.buy_iop = (LinearLayout) convertView.findViewById(R.id.lin_ins_select_iop);

            viewHolder.iv_buy_iop = (ImageView) convertView.findViewById(R.id.iv_ins_select_iop);

            viewHolder.ins_money = (InsSelectSpinner) convertView.findViewById(R.id.ins_select_spinner);

            viewHolder.select_money = (LinearLayout)convertView.findViewById(R.id.lin_ins_select_money);

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }


        viewHolder.name.setText(list.get(position).NAME);

        viewHolder.instruction.setText(list.get(position).ANNOTATION);

        viewHolder.suggestion.setText("购买建议："+list.get(position).SUGGESTION);

        if(list.get(position).ITEMS != null&&list.get(position).ITEMS.size() > 0) {

            viewHolder.ins_money.setString(list.get(position).ITEMS.get(0).name);

            viewHolder.ins_money.ClearSpinner();

            viewHolder.ins_money.createItem(list.get(position).ITEMS);

        }else {


            viewHolder.select_money.setVisibility(View.INVISIBLE);

        }

        viewHolder.buy.setImageResource(R.mipmap.switch_off);

        viewHolder.iv_buy_iop.setImageResource(R.mipmap.icon_pink_uncheck);

        for (int i = 0; i < InsSelectActivity.getSelected().size(); i++) {

            if (list.get(position).ID.equals(InsSelectActivity.getSelected().get(i).NAME)) {

                viewHolder.buy.setImageResource(R.mipmap.switch_on);

                for (int j = 0; j < list.get(position).ITEMS.size(); j++) {

                    if (InsSelectActivity.getSelected().get(i).M_ID.equals(list.get(position).ITEMS.get(j).id)) {

                        viewHolder.ins_money.setString(list.get(position).ITEMS.get(j).name);

                    }

                }

                if (InsSelectActivity.getSelected().get(i).IOP.equals("1")) {

                    viewHolder.iv_buy_iop.setImageResource(R.mipmap.icon_pink_check);

                }

            }

        }

        viewHolder.buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String id = "";

                if(list.get(position).ITEMS.size()>0){

                    id = list.get(position).ITEMS.get(0).id;

                }else {

                    id = list.get(position).ID;

                }

                if (!InsSelectActivity.isExit(list.get(position).ID)) {

                    InsSelectActivity.putSelect(list.get(position).ID, id, "1", 0);

                } else {

                    InsSelectActivity.delSelected(list.get(position).ID);

                }

                notifyDataSetChanged();

            }
        });

        viewHolder.ins_money.setOnItemClickListener(new CustomSpinner.OnSpinnerClickListener<GetInsList.ITEM>() {
            @Override
            public void onShow() {

                viewHolder.ins_money.dismiss();

            }

            @Override
            public void onItemClick(GetInsList.ITEM item) {

                InsSelectActivity.putSelect(list.get(position).ID, item.id, "1", 2);

                notifyDataSetChanged();

            }

        });

        viewHolder.buy_iop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String id = "";

                if(list.get(position).ITEMS.size()>0){

                    id = list.get(position).ITEMS.get(0).id;

                }else {

                    id = list.get(position).ID;

                }

                if (InsSelectActivity.isExit(list.get(position).ID)) {

                    if (InsSelectActivity.getItem(list.get(position).ID).IOP.equals("0")) {

                        InsSelectActivity.putSelect(list.get(position).ID, "0", "1", 1);

                    } else {

                        InsSelectActivity.putSelect(list.get(position).ID, "0", "1", 0);

                    }

                } else {

                    InsSelectActivity.putSelect(list.get(position).ID,id, "1", 1);

                }

                notifyDataSetChanged();

            }
        });

        return convertView;

    }

    class ViewHolder {

        private TextView name;

        private ImageView buy;

        private TextView instruction;

        private TextView suggestion;

        private ImageView iv_buy_iop;

        private LinearLayout buy_iop;

        private InsSelectSpinner ins_money;

        private LinearLayout select_money;

    }


    public abstract class RefreshLinster {

        public abstract void refresh(String num, String i);

    }


}
